new.bash

#!/usr/bin/env bash

function new_v(){
    run new version
}
function new_p(){
    run new project
}
function new_f(){
    run new feature
}

function new(){
    msg_header "Shorthand with:"
    msg_instruct "[bent new v], [bent new f], [bent new p]"
    n="$(color_li 0)"
    d="$(color_li 1)"
    f="${cOff}"
    prompt_choose_function \
        "# bent new [command] " \
        "new_version" "version" "Create a new version of your project" \
        "new_feature" "feature" "Work on a new feature" \
        "new_project" "project" "Start a new project" \
    ;
}

function new_help(){
    run new
}

function new_version(){
    is_project_dir || return

    header "Current versions for $(str_project_info)"
    run show versions
    msg;
    msg_ulist \
        "$(msg_instruct "You might use 'vbeta', 'v1', 'v2', and so on. ")" \
        "$(msg_instruct "Use only letters, numbers, hypens, periods, and underscores ")" \
    ;
    msg;
    
    prompt_quit "Enter new version name: " newVersion \
        && return

    git checkout -b ${newVersion}
    # @TODO should I skip the save here & JUST upload?
    prompt_yes_or_quit "Upload '${newVersion}'?" \
        && return
    run core upload
}

function new_feature(){
    is_project_dir || return
    ## @TODO add show single version & use that
    ## @TODO check for conflicting branch
    header "Unpolished feature "
    msg "    This works, but needs polish & may change in the future"

    curBranch="$(cur_branch)"

    header "Your current versions are: "
    run show versions

    prompt_quit "Enter new feature name: " newFeature \
        && return

    branch="${curBranch}_${newFeature}"
    msg
    git checkout -b ${branch}

    msg
    msg_instruct "[bent merge] later, to integrate this feature into your project."
    msg

    prompt_yes_or_quit "Upload '${branch}'?" \
        && return
    run core upload

    msg
    msg_instruct "[bent merge] later, to integrate this feature into your project."
    msg
}

function new_project(){
	# @TODO Check url for proper pattern
	# @TODO auto-remove `git clone` from url, if that's pasted too

    dir="$(project_dir)"
    curDir="$(pwd)"
    gitUser="$(git_user)"


    if [[ $curDir == $dir ]]; then
        header "Project Exists"
        msg_olist \
            "This is already a project directory. [cd some_other_dir] then try again." \
            "Or delete your local git folder with [rm -rf .git], then try again" \
            "This Project, online: $(internet_url)" \
            "This Project, directory: ${dir}" \
        ;
        msg
        return;
    fi
    if [[ -n $dir && $dir != "" ]];then 
        msg
        msg_notice "Already Inside Project"
        msg_olist \
            "Existing Project Dir: ${dir}" \
            "New Project Dir: $(pwd)" \
            "Nested projects can be weird" \
        ;
        msg
        prompt_yes_or_quit "Create Project Here? " \
            && return
    fi


    msg "";
    msg "  1. Create new project (repository) online "
    msg "     - $(url new_repo github)"
    msg "     - $(url new_repo bitbucket)"
    msg "     - $(url new_repo gitlab)"
    msg "  2. Configure the project online"
    msg "     a. A README is often good to have "
    msg "     c. You can read more about licenses at https://choosealicense.com/"
    msg "  3. Follow the prompts below"
    msg ""
    header "New Project Location:"
    msg "$(pwd)";
    msg
    prompt_quit "[enter] to continue" na -e \
        && return;
    msg ""
    msg "  1. Find your 'clone' ssh url."
    msg "     - Github w/ a README: Click 'Code' button then 'Use SSH' if shown. "
    msg "     - Github w/o a README: At the top, click 'SSH'."
    msg "     - Bitbucket w/o a README: Dropdown should be 'SSH'."
    msg "     - Bitbucket w/ a README: Click 'Clone' in the top-right."
    msg "  2. Copy the url that looks like ${cInstruct}git@githost.com:UserName/PROJECT-NAME.git${cOff}"
    msg "     - Do NOT copy 'git clone' if you see that before the url"
    msg ""
    
    msg_instruct "[ctrl-c] to exit"
    #@TODO auto-remove 'git clone' if it's pasted here
    prompt_quit "Paste your project url [ctrl+shift+v]: " projectUrl \
        && return

    msg_status "Setting up..."

    git init
    git remote add origin "${projectUrl}"

    msg_status "Downloading..."

    git pull origin master
    git fetch

    msg_status "Saving..."

    git add -A
    git commit -m "Project Initialized from local"
    # @TODO review default branch push name
        # Will this create issues between hosts? Hub uses main. Lab uses master, I think
        # What if there's an existing branch?
    git push origin main

    msg_status "Done!"
    msg
    msg "Try ${cInstruct}[bent ignore]${cOff} to download a premade .gitignore file"
    msg
    #@TODO Provide some information in case there is an error here

}